﻿//-----------------------------------------------------------------------
// <copyright file="Projects.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views.FullScreenPortrait
{
    using System;

    using Windows.ApplicationModel.DataTransfer;
    using Windows.Foundation;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Projects : Page
    {
        public Projects()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.Parameter is int)
            {
                this.DataContext = new ViewModels.Projects((int)e.Parameter);
            }
            else
            {
                var input = e.Parameter as Tuple<int, Models.Project>;
                this.DataContext = new ViewModels.Projects(input);
            }
        }

        /// <summary>
        /// Returns the rectangle visually occupied by the FrameworkElement passed as a
        /// parameter.
        /// </summary>
        /// <param name="element">The visual element whose rectangle we are getting.</param>
        /// <returns>The rectangular space occupied by the visual element passed in as a parameter.</returns>
        private static Rect GetElementRect(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);
            Point point = buttonTransform.TransformPoint(new Point());
            return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
        }

        /// <summary>
        /// Enable the user to right-click on a URL to copy it to the clipboard.
        /// </summary>
        /// <param name="sender">The HyperlinkButton that the user right-clicked on.</param>
        /// <param name="e">The routed event args, e.g. whether the user is using a mouse for input.</param>
        private async void HyperlinkButtonRightTapped(object sender, RightTappedRoutedEventArgs e)
        {
            // Create the context menu to display to the user.
            var menu = new PopupMenu();
            menu.Commands.Add(new UICommand("Copy URL"));

            // Present the context menu to the user, asynchronously waiting for input.
            await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));

            // Acquire access to the Windows clipboard.
            var dataPackage = new DataPackage();

            // Obtain the URL that the user right-clicked on.
            var hyperlinkButton = (HyperlinkButton)sender;
            dataPackage.SetText(hyperlinkButton.NavigateUri.AbsoluteUri);

            // Copy the URL to the clipboard.
            Clipboard.SetContent(dataPackage);
        }
    }
}
